package com.wqr.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqr.pojo.Channel;
import com.wqr.service.ChannelService;
import com.wqr.service.StoreService;

@Controller
public class ChannelController extends BaseController {
	@Autowired
    public ChannelService channelService;
	@Autowired
    public StoreService storeService;
	
	@RequestMapping("/manageChannel")
	public String channel(Model model,@RequestParam(value = "pageIndex",required = false) String pageIndex,
			@RequestParam(value = "pageSize",required = false) String pageSize) throws Exception {
		List<Channel> list = channelService.list();
		model.addAttribute("itemList", list);
		page(list, model, pageSize, pageIndex);
	    return "manageChannel"; 
	}
	@RequestMapping("/addChannel")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int addChannel(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			Channel channel) throws Exception {
		channel.setChannelTime(new Date());
		channel.setChannelScore(0);
		int a = channelService.insertChannel(channel);
		return a;
	}
	//@RequestMapping(value="/deleteChannel/{userStateId}",method=RequestMethod.GET)
	@RequestMapping("/deleteChannel")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int deleteChannel(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			Channel channel) throws Exception {
		//@pathVariable //将 URL 中占位符参数绑定到控制器处理方法的入参中 URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中。
		int a = channelService.delectChannel(channel.getChannelId());
		return a;
	}
	@RequestMapping("/updateChannel")
	@ResponseBody
	public Channel updateChannel(@RequestBody Channel channel) throws Exception {
		Channel itemList = channelService.selectByPrimaryKey(channel.getChannelId());
		return itemList;
	}
	@RequestMapping("/saveChannel")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int saveChannel(@RequestBody Channel channel) throws Exception {
		int a = channelService.updateChannel(channel);
		return a;
	}
	@RequestMapping("/searchChannel")
	@ResponseBody
	public List<Channel> searchChannel(@RequestBody Channel channel) throws Exception {
		if(channel.getChannelName() == ""){
			channel.setChannelName(null);
		}
		List<Channel> itemList = channelService.selectChannel(channel);
	    return itemList;
	}
	
	@RequestMapping("/allChannel")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public List<Channel> allChannel() throws Exception {
		List<Channel> list = channelService.listAll();
		return list;
	}
}
